package com.winsdom.controller;

import com.winsdom.anno.Authentication;
import com.winsdom.dto.CustomerBillingHeaderDTO;
import com.winsdom.enums.AuthenticationType;
import com.winsdom.result.Result;
import com.winsdom.service.ICustomerBillingHeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/header")
@RequiredArgsConstructor
@Api(tags = "抬头审核")
@Authentication(AuthenticationType.HEADING_REVIEW)
public class CustomerBillingHeaderController {

    @Autowired
    private ICustomerBillingHeaderService iCustomerBillingHeaderService;

    @Parameters({
            @Parameter(name = "buyerName", description = "购方名称", required = false),
            @Parameter(name = "customerName", description = "客户名称", required = false),
            @Parameter(name = "mobilePhone", description = "客户手机号", required = false),
            @Parameter(name = "createTime", description = "申请时间", required = false),
            @Parameter(name = "auditResult", description = "审核结果", required = false),
            @Parameter(name = "auditTime", description = "审核时间", required = false)
    })
    @ApiOperation("查询未审核列表")
    @GetMapping("/list")
    public Result headerPageList(
            @RequestParam(value = "买方名称", required = false) String buyerName,// 买方名称
            @RequestParam(value = "客户名称", required = false) String customerName,// 客户名称
            @RequestParam(value = "客户手机号", required = false) String mobilePhone,        // 客户手机号
            @RequestParam(value = "申请时间", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createTime) {


        CustomerBillingHeaderDTO customerBillingHeaderDTO = new CustomerBillingHeaderDTO();

        // 设置买家名称
        customerBillingHeaderDTO.setBuyerName(buyerName);
        // 设置客户名称
        customerBillingHeaderDTO.setCustomerName(customerName);
        // 设置移动电话
        customerBillingHeaderDTO.setMobilePhone(mobilePhone);
        // 设置创建时间
        customerBillingHeaderDTO.setCreateTime(createTime);

        // 调用iCustomerBillingHeaderService的headerPageList方法获取分页列表，并返回ResponseEntity的成功结果
        return Result.success(iCustomerBillingHeaderService.headerPageList(customerBillingHeaderDTO));



        }//
        @ApiOperation("查询已审核列表")
        @GetMapping("/list1")
        public Result header1 (
                @RequestParam(value = "买方名称", required = false) String buyerName,// 买方名称
                @RequestParam(value = "客户名称", required = false) String customerName,// 客户名称
                @RequestParam(value = "客户手机号", required = false) String mobilePhone,// 客户手机号
                @RequestParam(value = "申请时间", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createTime,//
                @RequestParam(value = "审核结果", required = false) Integer auditResult,// 审核结果
                @RequestParam(value = "审核时间", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime auditTime) {

            // 创建一个CustomerBillingHeaderDTO对象用于封装查询条件
            CustomerBillingHeaderDTO customerBillingHeaderDTO1 = new CustomerBillingHeaderDTO();

            // 设置买家名称
            customerBillingHeaderDTO1.setBuyerName(buyerName);
            // 设置客户名称
            customerBillingHeaderDTO1.setCustomerName(customerName);
            // 设置其他参数
            customerBillingHeaderDTO1.setMobilePhone(mobilePhone);
            customerBillingHeaderDTO1.setCreateTime(createTime);
            customerBillingHeaderDTO1.setAuditResult(auditResult);
            customerBillingHeaderDTO1.setAuditTime(auditTime);

            // 调用iCustomerBillingHeaderService的headerPageList方法获取分页列表，并返回ResponseEntity的成功结果
            return Result.success(iCustomerBillingHeaderService.header1(customerBillingHeaderDTO1));

    }
@ApiOperation("审核通过")
    @GetMapping("/approve/")
    public Result approve(@RequestParam("抬头ID") Long headerId) {
        try {
            CustomerBillingHeaderDTO dto = new CustomerBillingHeaderDTO();
            dto.setHeaderId(headerId); //
            dto.setAuditResult(1); // 审核结果为通过
            dto.setAuditTime(LocalDateTime.now()); // 设置审核时间为当前时间
            dto.setAuditStatus(1); // 设置审核状态为已审核

            iCustomerBillingHeaderService.updateAuditResult(dto);
            return Result.success("通过");
        } catch (Exception e) {
            // 日志记录错误信息
            e.printStackTrace();
            return Result.success("审核失败");
        }
    }
@ApiOperation("审核驳回")
    @GetMapping("/reject/")
    public Result reject(@RequestParam("抬头ID") Long headerId) {
        try {
            CustomerBillingHeaderDTO dto = new CustomerBillingHeaderDTO();
            dto.setHeaderId(headerId); //
            dto.setAuditResult(0); // 审核结果为驳回
            dto.setAuditStatus(1); // 设置审核状态为已审核
            dto.setAuditTime(LocalDateTime.now()); // 设置审核时间为当前时间

            iCustomerBillingHeaderService.updateAuditResult(dto);
            return Result.success("驳回成功");
        } catch (Exception e) {
            // 日志记录错误信息
            e.printStackTrace();
            return Result.success("驳回失败");
        }
    }
@ApiOperation("撤销审核")
    @GetMapping("/api/revokeAudit")
    public Result revokeAudit(@RequestParam("抬头ID") Long headerId) {
        try {
            CustomerBillingHeaderDTO dto = new CustomerBillingHeaderDTO();
            dto.setHeaderId(headerId); //
            dto.setAuditStatus(0); // 设置为待审核状态

            iCustomerBillingHeaderService.updateAuditStatus(dto);
            return Result.success("撤销审核成功");
        } catch (Exception e) {
            // 日志记录错误信息
            e.printStackTrace();
            return Result.success("撤销审核失败");
        }
    }


}
